package cevent.source.cloudcenter.gateway;/**
 * Created by Cevent on 2021/2/19.
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @author cevent
 * @description
 * @date 2021/2/19 15:54
 */
@SpringBootApplication
//@EnableEurekaClient
@EnableDiscoveryClient
public class GatewayApplication {
    private static final Logger LOG= LoggerFactory.getLogger(GatewayApplication.class);

    public static void main(String[] args) {
        SpringApplication application=new SpringApplication(GatewayApplication.class);
        Environment environment=application.run(args).getEnvironment();
        LOG.info("路由模块gateway启动");
        LOG.info("gateway地址：\t http://127.0.0.1:{}",environment.getProperty("server.port"));
    }

    /**
     * CORS-cross origin resource sharing跨域配置
     */
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        corsConfiguration.setAllowCredentials(Boolean.TRUE);
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        //注意springboot新版本取消了addAllowedOrigin，要使用addAllowedOriginPattern
        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.setMaxAge(3600L);
        UrlBasedCorsConfigurationSource corsConfigurationSource=new UrlBasedCorsConfigurationSource(new PathPatternParser());
        corsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        //返回对象CorsWebFilter
        return new CorsWebFilter(corsConfigurationSource);
    }
}
